from sklearn.preprocessing import LabelEncoder
from sklearn.metrics import roc_auc_score, classification_report
import pandas as pd
import joblib


# 读取数据
data = pd.read_csv("../../data/raw/test2.csv")

# 二分类特征进行独热编码
binary_cols = ['Gender', 'Over18', 'OverTime']
data = pd.get_dummies(data, columns=binary_cols, drop_first=True)

# 多分类特征进行更细致的大分类并赋予相应权重

# BusinessTravel 特征
# 频繁出差可能对工作和生活平衡影响较大，权重可适当提高
travel_mapping = {'Non - Travel': 0, 'Travel_Rarely': 1, 'Travel_Frequently': 3}
data['BusinessTravel'] = data['BusinessTravel'].map(travel_mapping)

# Department 特征
# 研发部门可能对专业技能要求高，销售部门注重业绩，人力资源部门侧重协调管理，可据此赋权
department_mapping = {'Research & Development': 0, 'Sales': 2, 'Human Resources': 1}
data['Department'] = data['Department'].map(department_mapping)

# EducationField 特征
# 生命科学和医学可能专业性较强，与研发等岗位相关性高，可赋予较高权重
education_mapping = {'Other': 0, 'Human Resources': 1, 'Marketing': 2, 'Technical Degree': 3, 'Medical': 4, 'Life Sciences': 4}
data['EducationField'] = data['EducationField'].map(education_mapping)

# JobRole 特征，分为管理岗和非管理岗
management_roles = ['Manufacturing Director', 'Research Director', 'Manager']
jobrole_mapping = {role: 1 if role in management_roles else 0 for role in data['JobRole'].unique()}
data['JobRole'] = data['JobRole'].map(jobrole_mapping)

# MaritalStatus 特征
# 已婚人士可能更稳定，单身人士可能更有精力投入新挑战，离异人士可能处于调整期，可按此赋权
marital_mapping = {'Single': 1, 'Married': 2, 'Divorced': 0}
data['MaritalStatus'] = data['MaritalStatus'].map(marital_mapping)

# 提取特征和标签
X = data[["BusinessTravel",
          "Department",
          "EducationField",
          "EnvironmentSatisfaction",
          "JobInvolvement",
          "JobLevel",
          "JobRole",
          "JobSatisfaction",
          "MaritalStatus",
          "OverTime_Yes",
          "StockOptionLevel",
          "WorkLifeBalance",

          "Age",
          "DistanceFromHome",
          "YearsSinceLastPromotion",
          "YearsInCurrentRole",
          "NumCompaniesWorked",
          "MonthlyIncome",
          "YearsWithCurrManager",
          "PercentSalaryHike",
          "TotalWorkingYears",
          ]
]
scale = joblib.load('scale_model.pkl')
X = scale.transform(X)
y = data["Attrition"]

# 加载模型
model = joblib.load('xgb_model.pkl')
y_pred = model.predict_proba(X)[:, 1]

# 评估模型 AUC: 0.8627787307032591
print("AUC:", roc_auc_score(y, y_pred))
